package com.six.apidemic.fragment.flow;

import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.activity.PersonActivity;
import com.six.apidemic.adapter.PersonAdapter;
import com.six.apidemic.bean.PersonListCallbackBean;
import com.six.apidemic.fragment.BaseFragment;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.listener.OnItemClickListener;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 流调人员首页
 * @author Administrator
 */
public class FlowFragment extends BaseFragment  {

    //列表相关代码
    private List<PersonListCallbackBean.ResultBean> listNames = new ArrayList<>();
    private PersonAdapter personAdapter;
    private RecyclerView rlModule;

    @Override
    protected void initView(View view) {
        rlModule = view.findViewById(R.id.rlModule);
        rlModule = view.findViewById(R.id.rlModule);

        rlModule.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        personAdapter = new PersonAdapter(getActivity(), listNames);
        rlModule.setAdapter(personAdapter);
        personAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                showToast(listNames.get(position).getName());
                startActivity(new Intent(getActivity(), PersonActivity.class).putExtra("module_name",listNames.get(position).getName()));
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        getPersonList();
    }

    @Override
    protected int findLayoutId() {
        return R.layout.fragment_flow;
    }

    /**
     * 获取人员列表
     */
    public void getPersonList(){
        HTTPAPI.getInstance().getPersonList(new StringCallback(){
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("获取人员列表成功-->"+ s);
                PersonListCallbackBean bean = GsonUtils.GsonToBean(s,PersonListCallbackBean.class);
                if (bean.getCode() == SUCC_CODE) {
                    listNames.clear();
                    listNames.addAll(bean.getResult());
                    personAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtils.e("获取人员列表失败-->"+ e.getMessage());
            }
        });
    }


}
